package com.woniuxy.rms.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.rms.advice.LogPoint;
import com.woniuxy.rms.pojo.Permission;
import com.woniuxy.rms.service.PermissionService;
import com.woniuxy.rms.util.DataResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.xml.crypto.Data;
import java.util.List;

/**
 * Auther: ChenGang  <br/>
 * Date: 2020/3/26 0026:上午 9:38  <br/>
 * Description:
 */
@Controller
@RequestMapping("/perm")
public class PesrmissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * Description : 加载权限列表  <br/>
     * ChangeLog : 1. 创建 (2020/3/26 0026 上午 10:19 [Administrator]);
     * @param currentPage
     * @param pageSize
     * @return com.woniuxy.rms.util.DataResult
    */
    @GetMapping("/list")
    @ResponseBody
    @LogPoint("查看权限")
    @RequiresPermissions("perm:select")
    public DataResult list(@RequestParam(defaultValue = "1",required = false,name = "page") Integer currentPage,
                           @RequestParam(defaultValue = "10",required = false,name = "limit") Integer pageSize){
        List<Permission> perms = permissionService.findByPage(currentPage,pageSize);
        PageInfo info = new PageInfo(perms);
        return DataResult.success(info);
    }
    /**
     * Description : 添加权限  <br/>
     * ChangeLog : 1. 创建 (2020/3/26 0026 上午 10:20 [Administrator]);
     * @param perm
     * @return com.woniuxy.rms.util.DataResult
    */
    @PostMapping("/save")
    @ResponseBody
    @LogPoint("添加权限")
    @RequiresPermissions("perm:save")
    public DataResult save(Permission perm){
        return permissionService.savePerm(perm);
    }
    /**
     * Description : 批量删除  <br/>
     * ChangeLog : 1. 创建 (2020/3/26 0026 上午 10:42 [Administrator]);
     * @param ids  id字符串
     * @return com.woniuxy.rms.util.DataResult
    */
    @DeleteMapping("/batchDel")
    @ResponseBody
    @LogPoint("批量删除权限")
    @RequiresPermissions("perm:delete")
    public DataResult batchDel(String ids){
        if (ids == null){
            return DataResult.failed("请选择数据！");
        }
        return permissionService.batchDel(ids);
    }

    /**
     * Description : 单条删除  <br/>
     * ChangeLog : 1. 创建 (2020/3/26 0026 下午 12:35 [Administrator]);
     * @param id
     * @return com.woniuxy.rms.util.DataResult
    */
    @DeleteMapping("/del")
    @ResponseBody
    @LogPoint("删除单条权限")
    @RequiresPermissions("perm:delete")
    public DataResult del(Integer id){
        return permissionService.delById(id);
    }
    /**
     * Description : 跳转到编辑页面  <br/>
     * ChangeLog : 1. 创建 (2020/3/26 0026 下午 1:11 [Administrator]);
     * @param id
     * @return com.woniuxy.rms.util.DataResult
    */
    @GetMapping("/editPerm")
    @ResponseBody
    public ModelAndView update(Integer id, ModelAndView model){
        //获取权限数据
        Permission perm = permissionService.findById(id);
        model.setViewName("/system/editPerm");
        model.addObject("perm",perm);
        return model;
    }
    @PutMapping("/update")
    @ResponseBody
    @LogPoint("更新权限")
    @RequiresPermissions("perm:update")
    public DataResult editPerm(Permission perm){
        return permissionService.editPerm(perm);
    }
}